	    function sleep(ms) {
	            return new Promise(resolve => setTimeout(resolve, ms));
	        }
	
	    var socket;
	    var current=0;
	    var total;
	    var beforetime;
	
	    function connect(){
	        var host = "ws://" + '42.192.91.114:9998/';
	        document.getElementById("btnConnect").value = "连接中";
	        document.getElementById("btnConnect").disabled = true;
	        socket = new WebSocket(host);
	        try {
	
	            socket.onopen = function (msg) {
	                document.getElementById("btnConnect").value = "连接成功";
					document.getElementById("btnConnect").disabled = true;
					document.getElementById("btnConnect").style.display='none';
					document.getElementById("oparea").style.display='block';
	                //document.getElementById("btnSend").disabled = "";
	                //alert("连接成功！");
					autogetURL();
	            };

	            socket.onmessage = function (msg) {
	                if (typeof msg.data == "string") {
	                    displayContent(msg.data);
	                    if(msg.data=="Receive:100%"){
	                        current=0;
	                        total=0;
	                    }
	                    else if(msg.data.substr(0,7)=="Receive"){
	                        var str1=msg.data.split(':')[1];
	                        var str2=str1.split('/')[0];
	                        picsend(parseInt(str2))
	                    }
	                }
	                else {
	                    alert("非文本消息");
	                }
	            };
	
	            socket.onerror = function (error) { //alert("Error："+ error.name); 
					swal("连接失败", "无法连接到服务器，请稍后再试", "error");
				};
	
	            socket.onclose = function (msg) {
	                document.getElementById("btnConnect").value = "连接服务器";
	                document.getElementById("btnConnect").disabled = "";
					document.getElementById("btnConnect").style.display='block';
					document.getElementById("oparea").style.display='none';
					//document.getElementById("oparea").innerHTML='与服务器断开连接，请刷新重试';
	                //document.getElementById("btnSend").disabled = true;//alert("连接关闭!");
	                 };
	        }
	        catch (ex) {
	            log(ex);
	        }
	    }
	
	    async function send() {
	        var str ='?URL='+document.getElementById("longurl").value;
	        socket.send(str);
	    }
	
	    async function picsend(pos){
	        beforetime=new Date().getTime();
	        current=pos;
	        socket.close();
	        connect();
	        while(document.getElementById("btnConnect").value != "连接成功") {await sleep(200);}
	        var str = document.getElementById("longurl").value;
	        socket.send(str.substring(pos));
	    }
	
	    window.onbeforeunload = function () {
	        try {
	            socket.close();
	            socket = null;
	        }
	        catch (ex) {
	        }
	    };
	
	    function $(id) { return document.getElementById(id); }
	

	
	    function displayContent(msg) {
			if(msg.includes("waiting for ")){
				tstr0=msg.replace("waiting for ",'');
				tstr1=tstr0.replace(" seconds",'');
				swal("请勿频繁操作！", "请等待"+tstr1+"秒", "error");
			}else if(msg.includes("http")){
				window.location.href=msg;
			}else{
				$("shorturl").value =msg;
			}
			
	    }
	    function onkey(event) { if (event.keyCode == 13) { send(); } }

function autogetURL(){
	var tempID = getQueryVariable("ID")
	if(tempID){
		var tempShort = '?ID=' +String(tempID);
		socket.send(tempShort);
}
}
function getQueryVariable(variable)
{
       var query = window.location.search.substring(1);
       var vars = query.split("&");
       for (var i=0;i<vars.length;i++) {
               var pair = vars[i].split("=");
               if(pair[0] == variable){return pair[1];}
       }
       return(false);
}


function copy()
{
    var tmpid=document.getElementById("shorturl");
	if (tmpid.value!=""){
		tmpid.select();
		document.execCommand("Copy");
		swal("复制成功:", tmpid.value, "success");
	}else{
		swal("文本框为空！", "请输先转换长域名", "error");
	}
}
function turntourl(){
	var temp_short = document.getElementById("shorturl").value;
	if (temp_short!=""){
		window.open('https://'+temp_short);
	}else{
		swal("文本框为空！", "请输先转换长域名", "error");
	}
	
}
function CHeck(){
	var temp_long=document.getElementById("longurl").value;
	
			if (temp_long!=" ") {
				if(temp_long.includes("http://")){
				   send();
				}else if(temp_long.includes("https://")){
					send();
				}else{
					swal("错误的输入！", "请输入正确网址", "error");
				}
			}else{
				swal("文本框不能为空！", "请输入需要转换的长域名", "error");
			}
			
	}